package ui.component
{

	import flash.display.Sprite;

	import globals.ButtonType;

	import util.DisplayUtil;
	import util.TextStyle;
	import util.UIUtil;

	public class Page extends Sprite
	{
		private var _min:uint = 1;
		private var _max:uint = 1;
		private var _curNum:uint = 1;
		private var _handler:Function;
		private var _textField:GameTextField;

		public function Page(handler:Function)
		{
			super();

			_handler = handler;
			addChildren();
		}

		private function addChildren():void
		{
			var preBtn:TextButton = UIUtil.getTextButton(0, "上一页", ButtonType.BUTTON_PAGE, pageHandler);
			this.addChild(preBtn);

			var nextBtn:TextButton = UIUtil.getTextButton(2, "下一页", ButtonType.BUTTON_PAGE, pageHandler);
			nextBtn.x = 90;
			this.addChild(nextBtn);

			_textField = new GameTextField();
			_textField.defaultTextFormat = TextStyle.getTextFormat(0xFFFFFF, 12, false, "center");
			_textField.width = nextBtn.x + nextBtn.width;
			_textField.text = "0/0";
			_textField.mouseEnabled = false;
			this.addChild(_textField);
		}

		private function pageHandler(button:TextButton):void
		{
			var oldCurNum:uint = _curNum;

			if(_handler != null)
			{
				switch(button.id)
				{
					case 0:
						_curNum--;
						break;
					case 1:
						break;
					case 2:
						_curNum++
						break;
				}

				if(_curNum < _min)
				{
					_curNum = _min;
				}

				if(_curNum > _max)
				{
					_curNum = _max;
				}

				if(oldCurNum != _curNum)
				{
					_handler(_curNum);
					_textField.text = _curNum + "/" + _max;
				}
			}
		}

		public function clear():void
		{
			DisplayUtil.removeAllChild(this, true);
		}

		public function get min():uint
		{
			return _min;
		}

		public function set min(value:uint):void
		{
			_min = value;
		}

		public function get max():uint
		{
			return _max;
		}

		public function set max(value:uint):void
		{
			_max = value;
			_textField.text = _curNum + "/" + _max;
		}

		public function get curNum():uint
		{
			return _curNum;
		}

		public function set curNum(value:uint):void
		{
			if(_curNum != value)
			{
				_curNum = value;

				if(_curNum < _min)
				{
					_curNum = _min;
				}

				if(_curNum > _max)
				{
					_curNum = _max;
				}

				_textField.text = _curNum + "/" + _max;

				if(_handler != null)
				{
					_handler(_curNum);
				}
			}
		}
	}
}